package com.xy.work.template.app.controller;

import com.xy.work.template.app.domain.req.LoginReq;
import com.xy.work.template.app.domain.vo.LoginVo;
import com.xy.work.template.app.service.ILoginService;
import com.xy.work.template.base.domain.LoginUser;
import com.xy.work.template.base.domain.api.ApiResult;
import com.xy.work.template.base.utils.CommonUtil;
import com.xy.work.template.common.annotation.NotLoginRequired;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @author niuniu
 * @Date: 2021/8/16 09:04
 * @Description: 完成登录验证相关操作
 */
@Api(tags = {"登录管理"})
@Slf4j
@RequestMapping("${spring.application.name}/login/")
@RestController
public class LoginController {
    @Autowired
    private ILoginService ILoginService;
    @ApiOperation(value = "手机号和密码、手机号和验证码 登录")
    @NotLoginRequired
    @PostMapping("phone")
    public ApiResult<LoginVo> login(@ApiParam @Validated @RequestBody LoginReq loginReq){
            log.info("登录请求信息：{}",loginReq);
            return ApiResult.ofSuccess(ILoginService.loginPhone(loginReq));
    }
    @ApiOperation(value = "退出登录")
    @GetMapping("signOut")
    public ApiResult<Map<String,Object>> signOut(){
        LoginUser loginUser=CommonUtil.getCheckCurrentLoginUser();
        log.info("退出登录用户信息：{}",loginUser);
        ILoginService.signOut(loginUser);
        Map<String,Object> data=new HashMap<>();
        return ApiResult.ofSuccess(data);
    }

}
